﻿Daan = window.Daan || {};

Daan.Doctor = {
    Url: {
        Search: "GetDoctorList",
        JudgAttention: "JudgAttention",
        AddAttention: "AddAttention",
        MyPatient: "GetMyPatient",
        CircleInfo: "/Services/AjaxHandler.ashx?action=Circle.GetCircleDetail",
        CreateQuestion: "/Services/AjaxHandler.ashx?action=Circle.createQuestion"
    },
    GetUrl: function (url) {
        return "/Services/AjaxHandler.ashx?action=Doctor." + url;
    },
    Message: {

},
pageSize: 10,
pageIndex: 1,
Search: function (sendData, options) {

    /// <summary>
    /// 获取医生列表
    /// </summary>
    /// <param name="sendData"> 
    ///  json对象，发送到handler的数据信息
    /// </param> 
    /// <param name="options"> 
    ///  参数选项，包括容器，更多信息，tip提示，loading指定
    /// </param> 
    var ops = {
        container: $("#doctorList"),
        loading: $("#loading"),
        tipResult: $("#tipResult"),
        moreMain: $("#moreMain"),
        needDyn: false
    };
    $.extend(ops, options);
    ops.loading.show();
    ops.tipResult.hide();
    if (sendData.pageIndex == 1) {
        ops.container.empty();
    }
    sendData.pageSize = this.pageSize;
    ops.moreMain.unbind("click");
    ops.moreMain.bind("click", function () {
        sendData.pageIndex++;
        Daan.Doctor.Search(sendData, options);
    });
    var url = Daan.Doctor.GetUrl(this.Url.Search);
    DaAn.Ajax(url, sendData, function (result) {
        ops.loading.hide();
        ops.moreMain.show();
        var jsonData = $.parseJSON(result);
        pageCount = Math.ceil(jsonData.count / Daan.Doctor.pageSize);
        if (pageCount == sendData.pageIndex) {
            ops.moreMain.hide();
        }
        if (jsonData.data && jsonData.data.length > 0) {
            for (var i = 0; i < jsonData.data.length; i++) {
                ops.container.append(tmplate.DataBind(jsonData.data[i], { Index: i + 1 }));
            }
            ops.container.show();

        } else {
            if (pIndex == 1) {
                ops.container.hide();
                ops.tipResult.show();
            }
            ops.moreMain.hide();
        }
    }, { type: "post" });
},
AddAttention: function (doctorId, callback) {
    /// <summary>
    /// 添加关注
    /// </summary>
    /// <param name="doctorId"> 
    ///  医生id
    /// </param> 
    /// <param name="callback"> 
    ///  回调函数
    /// </param>  
    var url = this.GetUrl(this.Url.AddAttention);
    DaAn.Ajax(url, { doctorId: doctorId }, function (result) {
        var josnData = DaAn.Deserialize(result);
        InvokeCallback(josnData, callback);
    }, { type: "post" });
},
JudgAttention: function (doctorId, callback) {
    /// <summary>
    /// 判断关注
    /// </summary>
    /// <param name="doctorId"> 
    ///  医生id
    /// </param> 
    /// <param name="callback"> 
    ///  回调函数
    /// </param> 
    var url = this.GetUrl(this.Url.JudgAttention);
    DaAn.Ajax(url, { doctorId: doctorId }, function (result) {
        var josnData = DaAn.Deserialize(result);
        InvokeCallback(josnData, callback);
    }, { type: "post" });
},

GetMyPatient: function (doctorId, callback) {
    /// <summary>
    /// 获取我的病人列表
    /// </summary>
    /// <param name="doctorId"> 
    ///  医生id
    /// </param>    
    /// <param name="callback"> 
    ///  回调函数
    /// </param> 
    var url = this.GetUrl(this.Url.MyPatient);
    DaAn.Ajax(url, { doctorId: doctorId }, function (result) {
        var josnData = DaAn.Deserialize(result);
        InvokeCallback(josnData, callback);
    }, { type: "post" });
},

CreateQuestion: function (circleId, content, toUserId, callback) {
    /// <summary>
    /// 创建问题
    /// </summary>
    /// <param name="circleId"> 
    ///  圈子id
    /// </param> 
    /// <param name="content"> 
    ///  问题内容
    /// </param>
    /// <param name="toUserId"> 
    ///  向谁提问，用户的id
    /// </param>
    /// <param name="callback"> 
    ///  回调函数
    /// </param> 
    var url = this.Url.CreateQuestion;
    DaAn.Ajax(url, { cid: circleId, content: content, touserid: toUserId }, function (result) {
        var josnData = DaAn.Deserialize(result);
        InvokeCallback(josnData, callback);
    }, { type: "post" });
},

//因在多个地方使用，单独封装
showCircleInfo: function (obj, circleId) {
    /// <summary>
    /// 展示圈子详细信息,tip提示信息
    /// </summary>
    /// <param name="obj"> 
    ///  圈子图标对象
    /// </param> 
    /// <param name="circleId"> 
    /// 圈子Id信息
    /// </param> 
    this.timer = null;
    clearTimeout(this.timer);
    obj = $(obj);
    var needShow = true; //判断鼠标是否已移出，已移出不需要显示
    var top = obj.offset().top;
    var left = obj.offset().left;
    var height = obj.height();
    var mark = true; //判断鼠标是否移动到提示框上
    var circleInfo = $("#circleInfo");
    if (circleInfo.length < 1) {
        $(document.body).append('<div id="circleInfo" class="w270 introduceBox clearfix none"></div>');
    }
    top += height + 5;
    circleInfo.html('<i class="icon-load"></i>');
    obj.unbind("mouseout");
    circleInfo.bind("mouseover", function () {
        mark = false;
    });
    obj.bind("mouseout", function () {
        this.timer = setTimeout(function () {
            if (mark) {
                needShow = false;
                circleInfo.hide();
            }
        }, 200);
    });
    circleInfo.css({ top: top, left: left, position: "absolute", zIndex: 100 });
    circleInfo.show();
    circleInfo.unbind("mouseleave");
    circleInfo.bind("mouseleave", function () {
        circleInfo.hide();
        needShow = false;
    });
    var url = this.Url.CircleInfo;
    DaAn.Ajax(url, { cid: circleId }, function (data) {
        eval("var result=" + data);
        var str = ['<div class="float-l margin-t5">',
            '<div class="w60">', '<a href="/circle/circlecontent.aspx?circleid=', result.data.GroupId, '" target="_blank" class="greenlink"> <img src="', result.data.HeadUrl == "" ? '/images/default-img/default-circle.jpg' : result.data.HeadUrl, '" class="img-bor"></a>', result.data.AccessPermission == 2 ? '<i class="icon-lock" title=该圈子需要申请才能加入"></i>' : '', '</div>',
            '</div>',
            '<div class="float-l w180 line-h35">',
            ' <p class="w180"><a href="/circle/circlecontent.aspx?circleid=', result.data.GroupId, '" target="_blank" class="greenlink">', result.data.Name, '</a></p>',
            ' <p>问题：<font class="colorBlue">', result.data.QuestionCount, '</font></p>',
            '<p>成员：<font>', result.data.MenmberCount, '</font>人</p>',
            '</div>',
            '<div class="w250 float-l margin-l10"> <p class="margin-t5">', result.data.Description.length > 50 ? result.data.Description.substring(0, 50) + "..." : result.data.Description, '</p></div>',
            '<div class="w270 float-l margin-t10 bgColor"> ',
            (result.join == false) ? '<font class="float-l colorOrange margin-l5">未加入' : '<font class="float-l colorGreen margin-l5">已加入', '</font>',
            (result.join == false) ? '<a class="btn orangeLet-w70 float-r margin-t5 margin-r5" href="javascript:void(0);" onclick="join(' + result.data.GroupId + ')"> 加入圈子' : '<a class="btn greenLet-w70 float-r margin-t5 margin-r5" target="_blank" href="/circle/circlecontent.aspx?circleid=' + result.data.GroupId + '"> 进入圈子', '</a></div>'
            ].join('');
        circleInfo.html(str);
        if (needShow) {
            circleInfo.show();
        }
    });
},


//创建问题
CreateQuestion: function (doctorId) {
    if (!islogin()) {
        DaAn.Login("/Expert/ExpertIndex.aspx");
    } else {
        dia = art.dialog.open("/Expert/QuestionCreate.aspx?doctorId=" + doctorId, { width: "450px", height: "325px", title: "提示", close: function () { } }).lock();
    }
}

}